package com.lizhe;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

/**
 * 类描述:
 * 常见的异常类型
 * DisabledAccountException（帐号被禁用）
 * LockedAccountException（帐号被锁定）
 * ExcessiveAttemptsException（登录失败次数过多）
 * ExpiredCredentialsException（凭证过期）等
 *
 * @author Administrator
 * @create 2022-05-16 21:56
 */
public class TestAuthenticator {
    public static void main(String[] args) {
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setRealm(new IniRealm("classpath:shiro.ini"));
        SecurityUtils.setSecurityManager(securityManager);

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "1234561");

        try {
            System.out.println(subject.isAuthenticated());
            subject.login(token);
            System.out.println(subject.isAuthenticated());
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("authentication error, this user does not exist");
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.out.println("wrong password!");
        }


    }
}
